home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 274_01.zip / A_TESTI.C < prev    next >
Text File  |  1993-04-01  |  2KB  |  93 lines

  1. /* Array Handling Package
  2. (C) Copyright 1985,1987,1988 James P. Cruse - All Rights Reserved
  3.  
  4. a_testi.c  
  5.  
  6. A Part of the User-Supported Array Handling package. Please see
  7. the file ARRAY.h for discussion concerning the registration and 
  8. usage of the package.
  9.  
  10. This program does a Quick test of some of the Array Package routines.
  11.  
  12. Tests using Integer operations.
  13.  
  14. Show that the sum of the first N terms of an arithmetic sequence 
  15.     sn = a + (n-1)*d
  16. is
  17.     Sn = (n/2)*(2*a + (n-1)*d)
  18.  
  19. */    
  20.  
  21. #include <stdio.h>
  22.  
  23. #include "array.h"
  24.  
  25. #define     ARRSIZE        50      /* Number of Elements in Array */
  26.  
  27.  
  28. /* globals for arithmetic series */
  29. #define        A        2
  30. #define        D        5
  31.  
  32. /* global arrays */
  33. int    sn_array[ARRSIZE];
  34.  
  35. main()
  36. {
  37. int     i;
  38. int    calc_sum;
  39. int     sum;
  40.  
  41. /* Test whether or not your compile can handle curly braces without */
  42. /* having a conditional prior to them */
  43. /* If your compiler generates an error, try replacing ARR_IF_NEEDED */
  44. /* with 1 in array.h */
  45.  
  46. ARR_IF1
  47. {        
  48. sum = ARRSIZE;        /* do something */
  49. } ARR_IF2
  50.  
  51.  
  52. printf("Test the sum of an arithmetic series is Sn = (n/2)*(2*a + (n-1)*d)\n");
  53. printf("\nFill Array with Series, then sum to verify.\n");
  54. printf("If true, then summing should result in a simply calculated value\n");
  55.  
  56. printf("Filling Arrays...\n");
  57.  
  58. printf("Calculating Arithmentic Series\n");
  59. a_i_scale(ARRSIZE,sn_array,D,A);    /* C already starts @ 0, no n-1 needed*/
  60.  
  61.  
  62. printf("Display some Values\n");
  63.  
  64. /* now display the some of them */
  65. printf("Results: [0] = %d, [1] = %d, [%d] = %d, [%d] = %d\n",    \
  66.     sn_array[0],sn_array[1],ARRSIZE/2,sn_array[ARRSIZE/2],    \
  67.     ARRSIZE-1,sn_array[ARRSIZE-1]);
  68.  
  69.  
  70. printf("Now Summing Array\n");
  71.  
  72. /* now add them all up */
  73. a_sum(ARRSIZE,sum,sn_array);
  74.  
  75.  
  76. printf("Calculating Answer\n");
  77.  
  78. calc_sum = (ARRSIZE*(2*A + (ARRSIZE - 1) * D))/2;    /* /2 last: roundoff */
  79.  
  80.  
  81. printf("Results:\n");
  82.  
  83. /* now results are? */
  84. printf("Total: %d, Calculated %d, Total-Calculated %d\n",    \
  85.     sum,calc_sum,sum-calc_sum);
  86.  
  87.  
  88. /* QED */
  89. printf("\nDone\n");
  90.  
  91. exit(0);
  92. }
  93.